package com.zl.tby.service.impl;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyEducationResource;
import com.zl.tby.repository.TbyEducationResourceRepo;
import com.zl.tby.service.ITbyEducationResourceService;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.annotation.Db;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 教育资源管理接口实现类
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyEducationResourceServiceImpl implements ITbyEducationResourceService {

    @Db
    private TbyEducationResourceRepo tbyEducationResourceRepo;

    @Override
    @Tran
    public R<TbyEducationResource> add(TbyEducationResource entity) {
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyEducationResourceRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyEducationResource> update(TbyEducationResource entity) {
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyEducationResourceRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public TbyEducationResource getDetail() {
        List<TbyEducationResource> dataList = tbyEducationResourceRepo.selectList(mp -> { mp.whereTrue(); });
        if (dataList == null || dataList.size() == 0) {
            return new TbyEducationResource();
        }
        return dataList.get(0);
    }
}
